package com.hollycrm.ahunicom.sheet.business.logic;

import java.util.Iterator;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;

import org.springframework.orm.hibernate.HibernateTemplate;

import com.hollycrm.ahunicom.sheet.ExtendDAO;
import com.hollycrm.ahunicom.sheet.domain.SheetContentField;
import com.hollycrm.ahunicom.sheet.maintenance.hibernate.TblSheetEventParameter;

public class IsRepeatComplainLogic extends ExtendDAO{
	
    private Session ss=null;
	
	public Object query(Object ob) {
	       HibernateTemplate ht = getHibernateTemplate();
	       SessionFactory sessionFactory = ht.getSessionFactory();

	     try {
	       ss = sessionFactory.openSession();
	       //Query q = ht.createQuery(ss, "select count(a) from TblSheetEventParameter a ");
	       String sheetFlowNo = (String) ob;
	       String fieldCode = "isRepeatComplain";
	       Query q = ss.createQuery("select a from SheetContentField a where a.sheetFlowNo = '" + sheetFlowNo +"'" +" and a.fieldCode= '" + fieldCode + "'");
	       Object object = null;
	       if(q != null){
	    	  Iterator irt = q.iterate();
	    	  if(irt.hasNext()){
	    		  object  = q.iterate().next();
	    	  }else{
	    		  return null;
	    	  }
	       }else{
	    	   return null;
	       }
	       
	       ss.flush();
	       
	       return object;
	     } catch (HibernateException ex) {
	        ex.printStackTrace();
	     }finally{
	    		if(ss!=null){
	    			try {
						ss.close();
					} catch (HibernateException e) {
						e.printStackTrace();
					}
	    		}
	     }
	     return null;

	}
	
	
	
	
	public Object save(Object ob) {
		
		HibernateTemplate ht = getHibernateTemplate();
		this.FormToDTOExchange(ob, this.getPojo());
		try {		
			//ss = sessionFactory.openSession();
			//java.io.Serializable id = ss.save((TblSheetEventParameter)this.getPojo());
			//ss.flush();

			java.io.Serializable id = ht.save((SheetContentField)this.getPojo());
			if(id != null){
				return id.toString();
			}else{
				return null;
			}
			
		}finally{
//      		if(ss!=null){
//      			try {
//  					ss.close();
//  					
//  				} catch (HibernateException e) {
//  					e.printStackTrace();
//  				}
//      		}
       }
	}
	
	public Object update(Object ob) {
		
		HibernateTemplate ht = getHibernateTemplate();
		this.FormToDTOExchange(ob, this.getPojo());
		try {		
			//ss = sessionFactory.openSession();
			//java.io.Serializable id = ss.save((TblSheetEventParameter)this.getPojo());
			//ss.flush();
			SheetContentField sheetcontentfield = (SheetContentField)query(((SheetContentField)this.getPojo()).getSheetFlowNo());
			if(sheetcontentfield != null){
				sheetcontentfield.setFieldValue(((SheetContentField)this.getPojo()).getFieldValue());
				ht.update(sheetcontentfield);
			}else{
				ht.save((SheetContentField)this.getPojo());
			}

			return null;
			
		}finally{
//      		if(ss!=null){
//      			try {
//  					ss.close();
//  					
//  				} catch (HibernateException e) {
//  					e.printStackTrace();
//  				}
//      		}
       }
	}
}
